import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getUserInfo, login } from '~/api/user'

export const useUserStore = defineStore('user', () => {
  const userInfo = ref(null)
  const userDataScope = ref([])
  const location = ref({})
  const isLogin = ref(false)

  function getUserProfile(userData) {
    return new Promise((resolve, reject) => {
      tt.getUserProfile({
        success(res) {
          tt.showLoading({
            title: '登录中',
          })
          login(userData, res.userInfo).then((res) => {
            if (res.code === 0) {
              isLogin.value = true
              userInfo.value = res.data
            }
            tt.hideLoading()
            resolve(res)
          })
        },
        fail(res) {
          const {
            errMsg,
            errNo,
          } = res
          let desc = `服务端错误:${errNo}`
          switch (errMsg) {
            case 'getUserProfile:fail network unavailable':
              desc = '网络不可用'
              break
            case 'getUserProfile:fail auth deny':
              desc = '用户拒绝授权'
              break
            case 'getUserProfile:fail privacy permission is not authorized':
              desc = '用户拒绝隐私协议授权'
              break
            case 'getUserProfile:fail api scope is not declared in the privacy agreement':
              desc = '隐私协议中未定义相关隐私信息类型'
              break
            case 'getUserProfile:fail feature is not supported in app':
              desc = '宿主不支持相关功能'
              break
            case 'getUserProfile:fail internal error':
              desc = '内部错误'
              break
            case 'getUserProfile:fail not login':
              desc = '宿主未登录'
              break
            case 'getUserProfile:fail invalid session':
              desc = '宿主登录，小程序未登录'
              break
            default:
              break
          }
          tt.showToast({
            title: desc,
            icon: 'fail',
          })
          reject(res.errMsg)
        },
      })
    })
  }

  //  登录
  function userLogin(isLogin = false) {
    return new Promise((resolve, reject) => {
      // 发起登录请求
      tt.login({
        success: async (res) => {
          if (isLogin) {
            login(res, {}).then((r) => {
              if (r.code === 0)
                userInfo.value = r.data
            })
          }
          resolve(res)
        },
        fail: (res) => {
          const {
            errMsg,
            errNo,
          } = res
          let desc = `服务端错误:${errNo}`
          switch (errMsg) {
            case 'login:fail network unavailable':
              desc = '网络不可用'
              break
            case 'login:fail feature is not supported in app':
              desc = '当前宿主不支持'
              break
            case 'login:fail app in background':
              desc = '当前处于后台'
              break
            case 'login:fail host login fail':
              desc = '宿主登录失败'
              break
            default:
              break
          }
          tt.showToast({
            title: desc,
            icon: 'fail',
          })
          reject(res.errMsg)
        },
      })
    })
  }

  function getUser(force = true) {
    getUserInfo().then((res) => {
      if (res.code === 0) {
        userInfo.value = res.data
      }
      else {
        if (force) {
          uni.switchTab({
            url: '/pages/tabbar/my/index',
          })
        }
      }
    })
  }

  function getMyLocation() {
    tt.getLocation({
      success: (res) => {
        location.value = res
      },
      fail() {
        uni.toast({
          title: '获取位置信息失败',
          icon: 'none',
        })
      },
    })
  }

  return {
    userLogin,
    userInfo,
    userDataScope,
    location,
    getMyLocation,
    getUserProfile,
    getUser,
    isLogin,
  }
}, {
  persist: true,
})
